package net.hn.hnms.biz.cockpit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.hn.hnms.biz.cockpit.domain.YearRisk;
import net.hn.hnms.biz.cockpit.domain.vo.OptionVo;
import net.hn.hnms.biz.cockpit.domain.vo.RiskControlVo;
import net.hn.hnms.biz.cockpit.mapper.YearRiskMapper;
import net.hn.hnms.biz.cockpit.service.IYearRiskService;
import net.hn.hnms.sys.common.core.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class YearRiskServiceImpl extends ServiceImpl<YearRiskMapper, YearRisk> implements IYearRiskService {

    @Override
    public RiskControlVo getRiskControl(String mineCode) {
        RiskControlVo riskControlVo = new RiskControlVo();
        String year = DateUtils.dateTimeNow(DateUtils.YYYY);
        List<YearRisk> yearRisks = baseMapper.selectList(new LambdaQueryWrapper<YearRisk>().eq(YearRisk::getMineCode, mineCode)
                .eq(YearRisk::getCheckPeriod, year));
        if (CollectionUtils.isNotEmpty(yearRisks)) {
            List<OptionVo> collect = yearRisks.stream().map(u -> new OptionVo(u.getRiskType(), u.getRiskCount())).collect(Collectors.toList());
            riskControlVo.setList(collect).setTotal(collect.stream().mapToInt(u -> Integer.parseInt(u.getValue())).sum());
        }
        return riskControlVo;
    }

}
